/**
* AutoSWF - Cody Cushing. March 26, 2009
* Copyright (c) 2009 Cody Cushing
* No use restrictions. Maintain this notice in all versions. 
**/

package com.autoswf.taskrunner
{
	import flexunit.framework.TestCase;

	public class TaskControllerTest extends TestCase
	{
		private var taskModel:MockTaskModel;
		private var taskController:TaskController;
		
		public function TaskControllerTest(methodName:String=null)
		{
			super(methodName);
		}
		
		override public function setUp():void
		{
			taskModel = new MockTaskModel();
			taskController = new TaskController(taskModel);
		}
		
		/**
		 * Tests that a required load gets loaded
		 */		
		public function test01():void
		{
			var loadRequest:MockRequest = new MockRequest();
			
			taskModel.mock.method("addLoadTask").withArgs(loadRequest).once;  /* primary test */
			
			taskController.addPrimary(loadRequest);
			taskController.runTasks();
			assertTrue(taskModel.mock.verify());
		}
		
		
		/**
		 * Tests that a background loaded gets loaded when there are no required loads
		 */		
		public function test02():void
		{
			var loadRequest:MockRequest = new MockRequest();
			
			taskModel.mock.method("addLoadTask").withArgs(loadRequest).once;  /* primary test */
			
			taskController.addBackground(loadRequest);
			taskController.runTasks();
			assertTrue(taskModel.mock.verify());
		}
		
		
		/**
		 * Tests that number of loads remains under the total allowed concurrencies. 
		 */		
		public function test03():void
		{
			var loadRequest:MockRequest = new MockRequest();
			
			taskModel.mock.method("addLoadTask").withArgs(loadRequest).exactly(2); /* primary test */
			
			taskController.addPrimary(loadRequest);
			taskController.addPrimary(loadRequest);
			taskController.addPrimary(loadRequest);
			
			taskController.runTasks();
			assertTrue(taskModel.mock.verify());
		}
		
		/**
		 * Tests pending items are loaded after complete. 
		 */		
		public function test04():void
		{
			var loadRequest:MockRequest = new MockRequest();
			
			taskModel.mock.method("addLoadTask").withArgs(loadRequest).exactly(3); /* primary test */
			
			taskController.addEventListener(TaskEvent.PRIMARY_TASK_COMPLETE, addAsync(function():void{}, 50));
			taskController.addPrimary(loadRequest);
			taskController.addPrimary(loadRequest);
			taskController.addPrimary(loadRequest);
			
			taskController.runTasks();
			
			taskModel.dispatchEvent(new TaskEvent(TaskEvent.TASK_COMPLETE, null));
			
			assertTrue(taskModel.mock.verify());
		}
		
	}
}